<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />   
        <title>Welcome to Design Zone......</title>
        <link rel="stylesheet" type="text/css" href="../../scripts/ext/resources/css/ext-all.css" />
        <link rel="stylesheet" type="text/css" href="../../styles/dzbase.css" />
        <script type="text/javascript" src="../../scripts/ext/adapter/ext-base.js">
        </script>
        <script type="text/javascript" src="../../scripts/ext/ext-all.js">
        </script>
        <script type="text/javascript" src="../../scripts/menu/EditableItem.js">
        </script>
		<script type="text/javascript" src="../../scripts/menu/RangeMenu.js">
        </script>
		<script type="text/javascript" src="../../scripts/grid/GridFilters.js">
        </script>
		<script type="text/javascript" src="../../scripts/grid/filter/Filter.js">
        </script>
		<script type="text/javascript" src="../../scripts/grid/filter/StringFilter.js">
        </script>
		<script type="text/javascript" src="../../scripts/grid/filter/DateFilter.js"></script>
		<script type="text/javascript" src="../../scripts/grid/filter/ListFilter.js"></script>
        <script type="text/javascript" src="../../scripts/i18nMessage.js">
        </script>
        <script type="text/javascript" src="../../scripts/utils.js">
        </script>
        <script type="text/javascript" src="../../scripts/selectYarn.js">
        </script>
        <script type="text/javascript">
        	var quMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.quotation');
        	var piMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.productidentification');
        	var yarnMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.yarn');
        	var fMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.factory');
            var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
            
            var ctSeasonNumberXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtSeasonNumber');
            var ctCurrencyXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtCurrency');
            
            var ctSeasonNumberStore = new Ext.data.Store({
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'code', 'description']),
					data: ctSeasonNumberXmlLoader.getXml()
                });
            var ctCurrencyStore = new Ext.data.Store({
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'code', 'description']),
					data: ctCurrencyXmlLoader.getXml()
                });
                
            var ctSeasonNumberCombo = new Ext.form.ComboBox({
                    fieldLabel: piMessage.get('ui.productidentification.seasonnumber'),
                    id: 'ctSeasonNumber',
                    name: 'ctSeasonNumber',
                    allowBlank: false,
                    blankText: message.get('message.edit.noblank'),
                    valueField: 'code',
                    typeAhead: true,
                    store: ctSeasonNumberStore,
                    displayField: 'description',
                    forceSelection: true,
                    triggerAction: 'all',
                    emptyText: message.get('message.combo.pleaseselect'),
                    selectOnFocus: true,
                    mode: 'local',
					listeners: {
						select: {
							fn: function (thisComb, record,index){
								//setReference();
							}
						}
					}
                });
                
             var ctCurrencyCombo = new Ext.form.ComboBox({
                    fieldLabel: quMessage.get('ui.quotation.currency'),
                    id: 'ctCurrency',
                    name: 'ctCurrency',
                    allowBlank: false,
                    blankText: message.get('message.edit.noblank'),
                    valueField: 'code',
                    typeAhead: true,
                    store: ctCurrencyStore,
                    displayField: 'description',
                    forceSelection: true,
                    triggerAction: 'all',
                    emptyText: message.get('message.combo.pleaseselect'),
                    selectOnFocus: true,
                    mode: 'local',
					listeners: {
						select: {
							fn: function (thisComb, record,index){
								//setReference();
							}
						}
					}
                });
                
            var selectYarnCallback = function(yarnId, yarnReference, factoryName, ctYarnMaterial1, ctYarnMaterial2, ctYarnMaterial3, ctYarnMaterial4, ctYarnMaterial5){
				//alert(yarnId + ' ' + factoryName + ' ' +yarnReference+ ' ' +ctYarnMaterial1+ ' ' +ctYarnMaterial2+ ' ' +ctYarnMaterial3+ ' ' +ctYarnMaterial4+ ' ' +ctYarnMaterial5);
				document.getElementById('yarnId').value = yarnId;
				document.getElementById('yarnReference').value = yarnReference;
				document.getElementById('factoryName').value = factoryName;
				/*document.getElementById('ctYarnMaterial1').value = ctYarnMaterial1;
				document.getElementById('ctYarnMaterial2').value = ctYarnMaterial2;
				document.getElementById('ctYarnMaterial3').value = ctYarnMaterial3;
				document.getElementById('ctYarnMaterial4').value = ctYarnMaterial4;
				document.getElementById('ctYarnMaterial5').value = ctYarnMaterial5;*/
			}

			var calculatePrice = function(index){
				var price = Ext.getCmp('price').getValue();
				var increase = Ext.getCmp('increase' + index).getValue();
				Ext.getCmp('price' + index).setValue(price * (100 + increase)/100);
			}

			var calculateMax = function(index){
				var pre = index - 1;
				var min = Ext.getCmp('min' + pre).getValue();
				Ext.getCmp('max' + index).setValue(min - 1);
			}
			
            Ext.onReady(function(){
                Ext.QuickTips.init();
                Ext.form.Field.prototype.msgTarget = 'side';
								
                var idValue = getPrarmeterValue(window.location, 'id');
                var startPage = getPrarmeterValue(window.location, 'startPage');
                var yarnForm = new Ext.FormPanel({
                    labelAlign: 'left',
                    labelWidth: 120, 
                    url: '../../servlet/QuYarnServlet?action=1',
                    frame: true,
                    reader: new Ext.data.XmlReader({
                        record: 'item'
                    }, [{
                        name: 'id',
                        mapping: 'id'
                    }, {
                        name: 'date',
                        mapping: 'date'
                    }, {
                        name: 'ctSeasonNumber',
                        mapping: 'ctSeasonNumber'
                    }, {
                        name: 'ctCurrency',
                        mapping: 'ctCurrency'
                    }, {
                        name: 'yarnId',
                        mapping: 'yarnId'
                    }, {
                        name: 'price',
                        mapping: 'price'
                    }, {
                        name: 'min1',
                        mapping: 'min1'
                    }, {
                        name: 'max1',
                        mapping: 'max1'
                    }, {
                        name: 'increase1',
                        mapping: 'increase1'
                    }, {
                        name: 'price1',
                        mapping: 'price1'
                    }, {
                        name: 'min2',
                        mapping: 'min2'
                    }, {
                        name: 'max2',
                        mapping: 'max2'
                    }, {
                        name: 'increase2',
                        mapping: 'increase2'
                    }, {
                        name: 'price2',
                        mapping: 'price2'
                    }, {
                        name: 'min3',
                        mapping: 'min3'
                    }, {
                        name: 'max3',
                        mapping: 'max3'
                    }, {
                        name: 'increase3',
                        mapping: 'increase3'
                    }, {
                        name: 'price3',
                        mapping: 'price3'
                    }, {
                        name: 'min4',
                        mapping: 'min4'
                    }, {
                        name: 'max4',
                        mapping: 'max4'
                    }, {
                        name: 'increase4',
                        mapping: 'increase4'
                    }, {
                        name: 'price4',
                        mapping: 'price4'
                    }, {
                        name: 'min5',
                        mapping: 'min5'
                    }, {
                        name: 'max5',
                        mapping: 'max5'
                    }, {
                        name: 'increase5',
                        mapping: 'increase5'
                    }, {
                        name: 'price5',
                        mapping: 'price5'
                    }, {
                        name: 'comment',
                        mapping: 'comment'
                    }]),
                    title: quMessage.get('ui.quotation.edityarn'),
                    bodyStyle: 'padding:5px 5px 5px 5px',
                    
                    items: [{
                        id: 'id',
                        name: 'id',
                        xtype: 'hidden'
                    },{
                        id: 'yarnId',
                        name: 'yarnId',
                        xtype: 'hidden'
                    },{
                        layout: 'column',
                        items: [{
                            columnWidth: .33,
                            layout: 'form',
                            items: [{
								id: 'date',
                                name: 'date',
                                fieldLabel: message.get('message.edit.date') + '(d/m/Y)',
                                xtype: 'datefield',
                                allowBlank: false,
                                blankText: message.get('message.edit.noblank'),
                                maxLength: 64
                            },{
                                id: 'yarnReference',
                                name: 'yarnReference',
                                xtype: 'textfield',
                                fieldLabel: yarnMessage.get('ui.yarn.reference'),
                                anchor: '86%',
								width: '90%',
                                allowBlank: false,
                                blankText: message.get('message.edit.noblank'),
								emptyText: message.get('message.combo.pleaseselect'),
                                maxLength: 64,
                                maxLengthText: message.get('message.edit.toolong'),
								readOnly: true,
                                listeners: {
                                    'render': {
                                        fn: function(){
                                            Ext.getCmp('yarnReference').getEl().on('click', function(){
                                                selectYarn(selectYarnCallback);
                                            }, this);
                                        },
                                        scope: this
                                    }
                                }
                            }]
						},{
                            columnWidth: .33,
                            layout: 'form',
                            items: [ctSeasonNumberCombo,{
                                id: 'factoryName',
                                name: 'factoryName',
                                xtype: 'textarea',
                                fieldLabel: fMessage.get('ui.factory.name'),
                                anchor: '86%',
								width: '90%',
								grow: true,
                                maxLengthText: message.get('message.edit.toolong'),
								readOnly: true
                            }]
						
						},{
                            columnWidth: .33,
                            layout: 'form',
                            items: [ctCurrencyCombo,{
								id: 'price',
                                name: 'price',
                                fieldLabel: quMessage.get('ui.quotation.yarnprice') + '&nbsp;/&nbsp;<b>Kg</b>',
                                xtype: 'numberfield',
                                decimalPrecision: 2,
                                anchor: '86%',
								width: '90%',
                                allowBlank: false,
                                blankText: message.get('message.edit.noblank')
                            }]
						
						}]
					},{
						xtype: 'fieldset',
						collapsible: true,
						title: quMessage.get('ui.quotation.lot'),
						autoHeight: true,
						items: [{
	                        layout: 'column',
	                        items: [{
	                            columnWidth: .25,
	                            labelWidth: 95, 
	                            layout: 'form',
	                            items: [{html: '<div class="div" style="margin-left: 100px;"><label class="x-form-item-label">' + quMessage.get('ui.quotation.min') + '</label></div><br/>'},{
                                    name: 'min1',
									id: 'min1',
                                    fieldLabel: quMessage.get('ui.quotation.lot') + '&nbsp;1',
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '86%',
                                    allowBlank: false,
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'min2',
									id: 'min2',
                                    fieldLabel: quMessage.get('ui.quotation.lot') + '&nbsp;2',
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '86%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'min3',
									id: 'min3',
                                    fieldLabel: quMessage.get('ui.quotation.lot') + '&nbsp;3',
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '86%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'min4',
									id: 'min4',
                                    fieldLabel: quMessage.get('ui.quotation.lot') + '&nbsp;4',
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '86%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'min5',
									id: 'min5',
                                    fieldLabel: quMessage.get('ui.quotation.lot') + '&nbsp;5',
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '86%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                }]
							},{
	                            columnWidth: .18,
	                            layout: 'form',
	                            items: [{html: '<div class="div"><label class="x-form-item-label">' + quMessage.get('ui.quotation.max') + '</label></div><br/>'},{
                                    name: 'max1',
									id: 'max1',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'max2',
									id: 'max2',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('max2').getEl().on('click', function(){
                                                	calculateMax(2);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'max3',
									id: 'max3',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('max3').getEl().on('click', function(){
                                                	calculateMax(3);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'max4',
									id: 'max4',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('max4').getEl().on('click', function(){
                                                	calculateMax(4);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'max5',
									id: 'max5',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('max5').getEl().on('click', function(){
                                                	calculateMax(5);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                }]
							},{
	                            columnWidth: .18,
	                            layout: 'form',
	                            items: [{html: '<div class="div"><label class="x-form-item-label">' + quMessage.get('ui.quotation.increase') + '&nbsp;&nbsp;<b>(%)</b></label></div><br/>'},{
                                    name: 'increase1',
									id: 'increase1',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    allowBlank: false,
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    value:0
                                },{
                                    name: 'increase2',
									id: 'increase2',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'increase3',
									id: 'increase3',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'increase4',
									id: 'increase4',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                },{
                                    name: 'increase5',
									id: 'increase5',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64
                                }]
							},{
	                            columnWidth: .18,
	                            layout: 'form',
	                            items: [{html: '<div class="div"><label class="x-form-item-label">' + quMessage.get('ui.quotation.yarnprice') + '&nbsp;/&nbsp;<b>Kg</b></label></div><br/>'},{
                                    name: 'price1',
									id: 'price1',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    allowBlank: false,
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('price1').getEl().on('click', function(){
                                                	calculatePrice(1);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'price2',
									id: 'price2',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('price2').getEl().on('click', function(){
                                                	calculatePrice(2);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'price3',
									id: 'price3',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('price3').getEl().on('click', function(){
                                                	calculatePrice(3);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'price4',
									id: 'price4',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('price4').getEl().on('click', function(){
                                                	calculatePrice(4);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                },{
                                    name: 'price5',
									id: 'price5',
                                    hideLabel: true,
                                    xtype: 'numberfield',
                                    decimalPrecision: 2,
                                    anchor: '80%',
                                    blankText: message.get('message.edit.noblank'),
                                    maxLength: 64,
                                    listeners: {
                                        'render': {
                                            fn: function(){
                                                Ext.getCmp('price5').getEl().on('click', function(){
                                                	calculatePrice(5);
                                                }, this);
                                            },
                                            scope: this
                                        }
                                    }
                                }]
							}]
						}]
					},{
                        fieldLabel: message.get('message.edit.comment'),
                        name: 'comment',
                        xtype: 'textarea',
                        maxLength: 1024,
						width: '90%',
                        maxLengthText: message.get('message.edit.toolong')
                    }],
                    
                    buttons: [{
                        text: message.get('message.edit.save'),
                        handler: function(){
                            if (yarnForm.getForm().isValid()) {
                                //messageForm.hide();
                            	document.getElementsByName('ctSeasonNumber')[0].value = ctSeasonNumberCombo.getValue();
                        		document.getElementsByName('ctCurrency')[0].value = ctCurrencyCombo.getValue();
                        		
                                yarnForm.getForm().submit({
                                    method: 'post',
                                    success: function(form, action){
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.successful'));
                                        yarnForm.getForm().reset();
                                        //messageForm.show();
                                    },
                                    failure: function(form, action){
                                        //alert(form);
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.unsuccessful'));
                                        //messageForm.show();
                                    }
                                });
                            }
                        }
                    },{
						text: message.get('message.edit.reset'),
						handler: function(){
							messageForm.getForm().reset();
						}
					}, {
                        text: message.get('message.edit.cancel'),
                        handler: function(){
                            if (startPage != null) {
                                window.location = 'yarn_list.html?startPage=' + startPage;
                            }
                            else {
                                window.location = 'yarn_list.html';
                            }
                        }
                    }]
                });
				
                yarnForm.render('yarnForm');
	
		
				if(idValue != null){
					document.getElementById('id').value = idValue;
					yarnForm.getForm().load({url:'../../servlet/QuYarnServlet?action=3&id=' + idValue, waitMsg:Ext.LoadMask.prototype.msg});
				}
            });
			
			
        </script>
    </head>
    <body>
        <div id="yarnForm"></div>
    </body>
</html>
